//
//  AuthenMiniLegalMsgVM.swift
//  kxf
//
//  Created by Mac on 2024/3/7.
//

import Foundation
import UIKit
import SwiftUI

class AuthenMiniLegalMsgVM: UIView {
    
    var selfHeight = RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*9
    var controller = WHBaseViewVC()
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var msgTitleVm : RegisterViewHeadTitleVM = {
        let vm = RegisterViewHeadTitleVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.titleLabel.text = "店主信息"
        return vm
    }()
    lazy var idNameVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.msgTitleVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "证件姓名"
        vm.textField.placeholder = "请输入证件姓名"
        vm.textField.keyboardType = .default
        vm.maxLength = 30
        return vm
    }()
    lazy var idNoVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.idNameVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "证件号码"
        vm.textField.placeholder = "请输入证件号码"
//        vm.textField.keyboardType = .default
        vm.maxLength = 30
        return vm
    }()
    lazy var idStartTimeVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.idNoVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "证件有效期始"
        vm.textField.placeholder = "请选择证件有效期始"
        vm.showArrowImg()
        vm.tapBlock = {()in
            self.choiceTimeAction()
        }
        return vm
    }()
    lazy var idEndTimeVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.idStartTimeVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "证件有效期至"
        vm.textField.placeholder = "请选择证件有效期至"
        vm.showArrowImg()
        vm.tapBlock = {()in
            self.choiceTimeEndAction()
        }
        return vm
    }()
    lazy var idAddressVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.idEndTimeVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "身份证地址"
        vm.textField.placeholder = "请输入身份证地址"
        vm.textField.keyboardType = .default
        vm.maxLength = 40
//        vm.showArrowImg()
        return vm
    }()
    lazy var bankNameVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.idAddressVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "银行名称"
        vm.textField.placeholder = "请填写银行名称"
        vm.textField.keyboardType = .default
        vm.maxLength = 30
//
        return vm
    }()
    lazy var bankBranchVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.bankNameVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "开户支行"
        vm.textField.placeholder = "请选择开户支行"
        vm.showArrowImg()
        vm.tapBlock = {()in
            let vc = AuthenBankBranchListVC()
            vc.tapBlock = {(dict)in
                AuthenMiniMsgModel.shareInstance.legalBankBranchCode = "\(dict["branchNo"]as? String ?? "")"
                AuthenMiniMsgModel.shareInstance.legalBankBranchName = "\(dict["branchBankName"]as? String ?? "")"
                self.bankBranchVm.setLabelContent(content: AuthenMiniMsgModel.shareInstance.legalBankBranchName)
            }
            self.controller.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
    lazy var bankNoVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.bankBranchVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "银行卡号"
        vm.textField.placeholder = "请输入银行卡号"
        vm.maxLength = 30
        vm.textField.keyboardType = .numberPad
        return vm
    }()
    lazy var phoneVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.bankNoVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "预留手机号"
        vm.textField.placeholder = "请输入预留手机号"
        vm.maxLength = 11
        vm.textField.keyboardType = .numberPad
        return vm
    }()
    
    lazy var datePickerVm : DatePickerVM = {
        let vm = DatePickerVM.init(frame: .zero)
        vm.isHidden = true
        
        return vm
    }()
}

extension AuthenMiniLegalMsgVM{
    func initUI() {
        addSubview(msgTitleVm)
        addSubview(idNameVm)
        addSubview(idNoVm)
        addSubview(idStartTimeVm)
        addSubview(idEndTimeVm)
        addSubview(idAddressVm)
        addSubview(bankNameVm)
        addSubview(bankBranchVm)
        addSubview(bankNoVm)
        addSubview(phoneVm)
        
        UIApplication.shared.keyWindow?.addSubview(datePickerVm)
    }
    func updateUIForRefuse() {
        idNameVm.textField.text = "\(AuthenMiniMsgModel.shareInstance.legalName)"
        idNoVm.textField.text = "\(AuthenMiniMsgModel.shareInstance.legalIdCardNo)"
        idStartTimeVm.setLabelContent(content: "\(AuthenMiniMsgModel.shareInstance.legalIdCardStartTime)")
        idEndTimeVm.setLabelContent(content: "\(AuthenMiniMsgModel.shareInstance.legalIdCardEndTime)")
        idAddressVm.textField.text = "\(AuthenMiniMsgModel.shareInstance.legalAddress)"
        bankNameVm.textField.text = "\(AuthenMiniMsgModel.shareInstance.legalBankName)"
        bankBranchVm.setLabelContent(content: "\(AuthenMiniMsgModel.shareInstance.legalBankBranchName)")
        bankNoVm.textField.text = "\(AuthenMiniMsgModel.shareInstance.legalBankNo)"
        phoneVm.textField.text = "\(AuthenMiniMsgModel.shareInstance.legalBankPhone)"
    }
    func setReadMode(){
        idStartTimeVm.arrowImgView.isHidden = true
        idEndTimeVm.arrowImgView.isHidden = true
        bankBranchVm.arrowImgView.isHidden = true
        self.isUserInteractionEnabled = false
    }
}

extension AuthenMiniLegalMsgVM{
    func choiceTimeAction(){
        self.datePickerVm.isHidden = false
        self.datePickerVm.titleLabel.text = "证件有效期始"
        self.datePickerVm.timeConfirmBlock = {(time)in
            AuthenMiniMsgModel.shareInstance.legalIdCardStartTime = Date().changeDateFormatter(dateString: time)
            self.idStartTimeVm.setLabelContent(content: AuthenMiniMsgModel.shareInstance.legalIdCardStartTime)
            self.choiceTimeEndAction()
        }
    }
    func choiceTimeEndAction(){
        let alertVc = UIAlertController(title: "身份证是否长期有效", message: "", preferredStyle: .alert)
        
        let okAction = UIAlertAction(title: "长期有效", style: .default, handler: { (action)in
            AuthenMiniMsgModel.shareInstance.legalIdCardEndTime = "9999-01-01"
            self.idEndTimeVm.setLabelContent(content: AuthenMiniMsgModel.shareInstance.legalIdCardEndTime)
        })
        alertVc.addAction(okAction)
        let cancelAction = UIAlertAction(title: "非长期", style: .cancel, handler:{ (action)in
            self.datePickerVm.isHidden = false
            self.datePickerVm.titleLabel.text = "证件有效期至"
            self.datePickerVm.timeConfirmBlock = {(time)in
                AuthenMiniMsgModel.shareInstance.legalIdCardEndTime = Date().changeDateFormatter(dateString: time)
                self.idEndTimeVm.setLabelContent(content: AuthenMiniMsgModel.shareInstance.legalIdCardEndTime)
            }
        })
        alertVc.addAction(cancelAction)
        self.controller.present(alertVc, animated:true, completion:nil)
    }
}

